using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_TabGroupBtn), true)]
public class HMI_TabGroupBtn_Editor : HMI_EditorBase
{
    private HMI_TabGroupBtn mTarget;

    public TabGroupBtn mTabGroupBtn; //组
    private RectTransform mLayoutRoot;
    private RectTransform mBar;  //滑动条
    private SerializedProperty Texts;
    public override void Awake()
    {
        base.Awake();
        mTarget = target as HMI_TabGroupBtn;
        mTabGroupBtn = mTarget.transform.Find("Group").GetComponent<TabGroupBtn>();
        mLayoutRoot = mTabGroupBtn.GetComponent<RectTransform>();
        mBar = mTarget.transform.Find("Bar").GetComponent<RectTransform>();

        mTarget.mTexts = new List<string>(mLayoutRoot.childCount);
        for (int i = 0; i < mLayoutRoot.childCount; i++)
        {
            var text = mLayoutRoot.GetChild(i).transform.GetComponent<Text>();
            mTarget.mTexts.Add(text.text);
        }
    }

    private void OnEnable()
    {
        Texts = serializedObject.FindProperty("mTexts");
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(Texts);
        mTarget.hasAni = EditorGUILayout.Toggle("使用动画：", mTarget.hasAni);
        mTabGroupBtn.hasAni = mTarget.hasAni;

        if (EditorGUI.EndChangeCheck())
        {
            RefreshTexts();
            var voiceText = "";
            if (mTarget.mTexts != null)
            {
                for (int i = 0; i < mTarget.mTexts.Count; i++)
                {
                    voiceText += (mTarget.mTexts[i] + "|");
                }
            }
            mTarget.SetVoiceText(voiceText);
        }
    }

    protected override string GetDataJson()
    {
        var data = new HMI_TabGroupBtnData();

        data.mTexts = mTarget.mTexts;
        data.hasAni = mTarget.hasAni;

        string json = JsonUtility.ToJson(data, true);
        return json;
    }

    protected override void SetData(string json)
    {
        HMI_TabGroupBtnData data = JsonUtility.FromJson<HMI_TabGroupBtnData>(json);

        mTarget.mTexts = data.mTexts;
        mTarget.hasAni = data.hasAni;
        RefreshTexts();

        base.SetData(json);
        EditorUtility.SetDirty(mTarget);
    }

    private void RefreshTexts()
    {
        mTabGroupBtn.Text_Btns.Clear();
        var oldCount = mLayoutRoot.childCount;
        if (mTarget.mTexts == null || mTarget.mTexts.Count <= 0)
        {
            mLayoutRoot.GetChild(0).gameObject.SetActive(false);
            for (int i = 1; i < oldCount; i++)
            {
                DestroyImmediate(mLayoutRoot.GetChild(i).gameObject);
            }
        }
        else
        {
            if (oldCount > mTarget.mTexts.Count)
            {
                for (int i = mTarget.mTexts.Count; i < oldCount; i++)
                {
                    DestroyImmediate(mLayoutRoot.GetChild(i).gameObject);
                }
            }
            else if (oldCount < mTarget.mTexts.Count)
            {
                var delta = mTarget.mTexts.Count - oldCount;
                for (int i = 0; i < delta; i++)
                {
                    Instantiate(mLayoutRoot.GetChild(0).gameObject, Vector2.zero, Quaternion.identity, mLayoutRoot);
                }
            }

            for (int i = 0; i < mTarget.mTexts.Count; i++)
            {
                var item = mLayoutRoot.GetChild(i);
                item.gameObject.SetActive(true);
                item.gameObject.name = mTarget.mTexts[i];
                var text = item.GetComponent<Text>();
                text.text = mTarget.mTexts[i];
                mTabGroupBtn.Text_Btns.Add(item.GetComponent<Button>());
            }
        }
    }
}

[Serializable]
public class HMI_TabGroupBtnData
{
    public List<string> mTexts;  //按钮列表
    public bool hasAni;  //是否使用动画
}
